#!/bin/bash
#usage: ./run.sh <test config file> <runs> <results dest>
#  runs         = number of test runs
#  results dest = name for a directory where results are put

config=$1
runs=$2
results_dir=$3

source $config


source export.sh $config

echo "Removing previous results..."
ssh -p $SGNL_INITIATOR_PORT root@$SGNL_INITIATOR "rm -rf $basedir/results/*; rm -rf $basedir/$SGNL_TEST_NAME"
echo "Removed previous results from Initiator..."
ssh -p $SGNL_RESPONDER_PORT root@$SGNL_RESPONDER "rm -rf $basedir/results/*; rm -rf $basedir/$SGNL_TEST_NAME"
echo "Removed previous results from Responder..."
if [ $SGNL_HIPFW_MBOX_OPTIONS ]; then
    ssh -p $SGNL_MBOX1_PORT root@$SGNL_MBOX      "rm -rf $basedir/results/*; rm -rf $basedir/$SGNL_TEST_NAME"
fi
echo "Removed previous results from Mbox1..."
if [ $SGNL_HIPFW_MBOX2_OPTIONS ]; then
    ssh -p $SGNL_MBOX2_PORT root@$SGNL_MBOX2     "rm -rf $basedir/results/*; rm -rf $basedir/$SGNL_TEST_NAME"
fi
echo "Removed previous results from Mbox2..."

echo "Running tests from config $config"


for i in `seq 1 $runs`; do
    echo "Running test #$i"
    screen -c $screen_cfg
done

echo "Performing network test"
basedir=$(dirname $SGNL_HIPD)
#ssh root@$SGNL_INITIATOR  "ping -c $runs $SGNL_RESPONDER > tmp_ping; cat tmp_ping | grep icmp_req | cut -d ' ' -f 8 | cut -d '=' -f2 > $basedir/results/PERF_PING.csv; rm tmp_ping"

echo "Moving results..."

ssh -p $SGNL_INITIATOR_PORT root@$SGNL_INITIATOR "rm -rf $basedir/$SGNL_TEST_NAME/$results_dir; mkdir $basedir/$SGNL_TEST_NAME; \
                          mv $basedir/results $basedir/$SGNL_TEST_NAME/$results_dir; mv $basedir/*log* $basedir/$SGNL_TEST_NAME/$results_dir"
echo "Moved results for initiator..."
ssh -p $SGNL_RESPONDER_PORT root@$SGNL_RESPONDER "rm -rf $basedir/$SGNL_TEST_NAME/$results_dir; mkdir $basedir/$SGNL_TEST_NAME; \
                          mv $basedir/results $basedir/$SGNL_TEST_NAME/$results_dir; mv $basedir/*log* $basedir/$SGNL_TEST_NAME/$results_dir"
echo "Moved results for responder..."

if [ $SGNL_HIPFW_MBOX_OPTIONS ]; then
    ssh -p $SGNL_MBOX1_PORT root@$SGNL_MBOX "rm -rf $basedir/$SGNL_TEST_NAME/$results_dir; mkdir $basedir/$SGNL_TEST_NAME; \
                             mv $basedir/results $basedir/$SGNL_TEST_NAME/$results_dir; mv $basedir/*log* $basedir/$SGNL_TEST_NAME/$results_dir"
    #ssh root@$SGNL_MBOX      "rm -rf $basedir/$SGNL_TEST_NAME/$results_dir; mkdir $basedir/$SGNL_TEST_NAME; \
    #                         mv $basedir/results $basedir/$SGNL_TEST_NAME/$results_dir; mv $basedir/*log* $basedir/$SGNL_TEST_NAME/$results_dir"
fi
echo "Moved results for middlebox1..."
if [ $SGNL_HIPFW_MBOX2_OPTIONS ]; then
    ssh -p $SGNL_MBOX2_PORT root@$SGNL_MBOX2     "rm -rf $basedir/$SGNL_TEST_NAME/$results_dir; mkdir $basedir/$SGNL_TEST_NAME; \
                              mv $basedir/results $basedir/$SGNL_TEST_NAME/$results_dir; mv $basedir/*log* $basedir/$SGNL_TEST_NAME/$results_dir"
fi
echo "Moved results for middlebox2..."

